﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class MasterPages_Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PreRender += new EventHandler(InitPage);

            Freeze();
        }
    }

    protected void InitPage(object sender, EventArgs e)
    {
        InitData();
    }
    protected void Freeze()
    {
        username_textbox.ReadOnly = true;
        pwd_textbox.ReadOnly = true;
        repwd_textbox.ReadOnly = true;
        idcard_textbox.ReadOnly = true;
        restaurant_name_textbox.ReadOnly = true;
        address_textbox.ReadOnly = true;
        tel_textbox.ReadOnly = true;
        mobile_textbox.ReadOnly = true;
        submit_button.Enabled = false;
        start_time_textbox.ReadOnly = true;
        close_time_textbox.ReadOnly = true;
        state_dropdownlist.Enabled = false;
        adver_textbox.ReadOnly = true;

        editcancel_button.Text = "编 辑";
    }
    protected void UnFreeze()
    {
        //username_textbox.ReadOnly = false;
        pwd_textbox.ReadOnly = false;
        repwd_textbox.ReadOnly = false;
        //idcard_textbox.ReadOnly = false;
        restaurant_name_textbox.ReadOnly = false;
        address_textbox.ReadOnly = false;
        tel_textbox.ReadOnly = false;
        mobile_textbox.ReadOnly = false;
        submit_button.Enabled = true;
        start_time_textbox.ReadOnly = false;
        close_time_textbox.ReadOnly = false;
        state_dropdownlist.Enabled = true;
        adver_textbox.ReadOnly = false;

        editcancel_button.Text = "取 消";
    }

    protected void InitData()
    {
        restaurant_name_textbox.Text = Session["name_restaurant"].ToString();
        address_textbox.Text = Session["address_restaurant"].ToString();
        tel_textbox.Text = Session["tel_restaurant"].ToString();
        adver_textbox.Text = Session["adver_restaurant"].ToString();
        start_time_textbox.Text = Session["start_time_restaurant"].ToString();
        close_time_textbox.Text = Session["close_time_restaurant"].ToString();
        state_dropdownlist.SelectedValue = Session["is_open_restaurant"].ToString();
        username_textbox.Text = Session["username_owner"].ToString();
        pwd_textbox.Text = Session["pwd_owner"].ToString();
        idcard_textbox.Text = Session["identity_card_owner"].ToString();
        mobile_textbox.Text = Session["mobile_owner"].ToString();
    }
    protected bool IsLogined()
    {
        return (Session["name_restaurant"] != null);
    }

    protected void UpdateNotice()
    {
        if (Session["notice_restaurant_basicinfo"] != null)
        {
            notice_lable.Text = Session["notice_restaurant_basicinfo"].ToString();
        }
    }

    //protected bool UpdateRestaurant(String username, String pwd, String restaurant_name,
    // String address, String tel, String phone, String start_time, String close_time, String remark, String is_open)
    //{

    //    String sqlUpdateOwner = "UPDATE owner set pwd='"+pwd+"',phone='"+phone+"' "+
    //                            "WHERE name='"+username+"'";

    //    String sqlUpdateRestaurant = "UPDATE restaurant "+
    //                                 "SET name='"+restaurant_name+"',address='"+address+"',tel='"+tel+"',"+
    //                                 "remark='"+remark+"',start_time='"+start_time+"',close_time='"+close_time+"',is_open="+is_open+
    //                                 "FROM restaurant,owner " + 
    //                                 " WHERE owner.name='" + username + "' and owner.id=restaurant.owner_id";

    //    if (!DB.QueryAlter(sqlUpdateOwner))
    //    {
    //        Session["notice_restaurant_basicinfo"] = "更新用户信息出错!";
    //        return false;
    //    }

    //    if (!DB.QueryAlter(sqlUpdateRestaurant))
    //    {
    //        Session["notice_restaurant_basicinfo"] = "更新餐厅信息出错!";
    //        return false;
    //    }

    //    return true;
    //}
  
    protected void OnSubmitBtnClick(object sender, EventArgs e)
    {
        //String username = username_textbox.Text.Trim();
        //String pwd = pwd_textbox.Text.Trim();
        //String repwd = repwd_textbox.Text.Trim();
        //String identity_card = idcard_textbox.Text.Trim();
        //String restaurant_name = restaurant_name_textbox.Text.Trim();
        //String address = address_textbox.Text.Trim();
        //String tel = tel_textbox.Text.Trim();
        //String phone = mobile_textbox.Text.Trim();
        //String start_time = start_time_textbox.Text.Trim();
        //String close_time = close_time_textbox.Text.Trim();
        //String remark = adver_textbox.Text.Trim();
        //String is_open = state_dropdownlist.SelectedValue.Trim();

        //if (!pwd.Equals(repwd))
        //{
        //    Session["notice_restaurant_basicinfo"] = "两次密码输入不一致！";
        //    UpdateNotice();
        //    return;
        //}

        //if (UpdateRestaurant(username, pwd, restaurant_name, address, tel, phone,
        //    start_time, close_time, remark, is_open))
        //{
        //    Session["notice_restaurant_basicinfo"] = "更新成功!";
        //    Freeze();
        //}

        //UpdateNotice();
    }
    protected void OnEditCancelBtnClick(object sender, EventArgs e)
    {
        //if (pwd_textbox.ReadOnly == true)
        //{
        //    UnFreeze();
        //    Session["notice_restaurant_basicinfo"] = "请编辑.";
        //}
        //else
        //{
        //    InitData();
        //    Freeze();
        //    Session["notice_restaurant_basicinfo"] = "已取消编辑.";
        //}

        //UpdateNotice();
    }
}
